<?php

namespace app\admin\controller\api;

use app\common\model\User;

/**
 * 学生
 * @internal
 */
class Student extends Api
{
    protected $noNeedLogin = ['index', 'info', 'getByCardNumber', 'profile', 'getProfileByIds'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    public function getById()
    {
        $id = $this->request->request('id', 0);
        $result = \app\common\model\Student::get($id);

        if ($result) {
            $result = $result->append(['gender_text'])->toArray();
            $this->success('', $result);
        } else {
            $this->error('该学生不存在');
        }
    }

    public function index()
    {
        $id = $this->request->request('id', 0);
        $result = \app\common\model\Student::get($id);

        if ($result) {
            $result = $result->append(['gender_text'])->toArray();
            $this->success('', $result);
        } else {
            $this->error('该学生不存在');
        }
    }

    public function info()
    {
        $id = $this->request->request('id', 0);
        $result = \app\common\model\Student::alias('student')
            ->join('__SCHOOL__ school', 'school.id = student.school_id')
            ->join('__CLASSES__ classes', 'classes.id = student.class_id')
            ->join('__GRADE__ grade', 'grade.id = student.grade_id')
            ->join('__CLASSROOM__ classroom', 'classroom.id = classes.classroom_id', 'left')
            ->join('__BUILDING__ building', 'building.id = classroom.building_id', 'left')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
            ->where('student.id', $id)
            ->field('student.id,student.number,student.name,student.avatar,student.school_id,school.name school_name,student.class_id,classes.name class_name,student.grade_id,grade.name grade_name,classes.classroom_id,classroom.name classroom_name,building.id building_id,building.name building_name,garden.id garden_id,garden.name garden_name')
            ->find();
        if ($result) {
            $this->success('', $result);
        } else {
            $this->error('该学生不存在');
        }
    }

    /**
     * 根据卡号获取学生信息
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getByCardNumber()
    {
        $card_number = $this->request->request('card_number');
        $class_id = $this->request->request('class_id');

        if (!$card_number || !$class_id) {
            $this->error('参数错误');
        }

        $student = \app\common\model\Student::alias('student')
            ->join('__CLASS_STUDENT__ class_student', 'class_student.student_id = student.id')
            ->where('student.card_number', $card_number)
            ->where('class_student.class_id', $class_id)
            ->field('student.id,student.card_number,student.avatar,student.number,student.name,class_student.class_id')
            ->find();

        if (!$student) {
            $this->error('数据不存在');
        }
        if (!strstr($student->avatar, 'http') && strstr($student->avatar, '/uploads')) {
            $student->avatar = config('app_url') . $student->avatar;
        }
        $this->success('', $student);
    }

    /**
     *  根据ids获取学生信息
     */
    public function profile()
    {
        $student_ids = $this->request->request('ids/a');

        $list = \app\common\model\Student::alias('student')
            ->join('__CLASSES__ classes', 'classes.id = student.class_id')
            ->join('__GRADE__ grade', 'grade.id = student.grade_id')
            ->join('__CLASSROOM__ classroom', 'classroom.id = classes.classroom_id', 'left')
            ->join('__BUILDING__ building', 'building.id = classroom.building_id', 'left')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
            ->whereIn('student.id', $student_ids)
            ->column('student.id,student.number,student.name,student.avatar,classes.name class_name,grade.name grade_name,classes.classroom_id,classroom.name classroom_name,building.id building_id,building.name building_name,garden.id garden_id,garden.name garden_name', 'student.id');

        foreach ($list as $key => $student) {
            if (!strstr($student['avatar'], 'http') && strstr($student['avatar'], 'uploads/')) {
                $list[$key]['avatar'] = config('app_url') . $student['avatar'];
            }
        }

        $this->success('', $list);
    }

    public function profiles()
    {
        $ids = $this->request->request('ids/a');

        $list = \app\common\model\Student::alias('student')
            ->join('__CLASSES__ classes', 'classes.id = student.class_id')
            ->join('__GRADE__ grade', 'grade.id = student.grade_id')
            ->join('__SCHOOL__ school', 'school.id = student.school_id')
            ->whereIn('student.id', $ids)
            ->column('student.id,student.number,student.name,student.gender,student.avatar,school.name school_name,grade.name grade_name,classes.name class_name', 'student.id');

        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['gender_text'] = User::getGender($value['gender']);
                if (!strstr($value['avatar'], 'http') && strpos($value['avatar'], 'uploads/') > -1) {
                    $list[$key]['avatar'] = config('app_url') . $value['avatar'];
                }
            }
        }

        $this->success('', $list);
    }

    public function lists()
    {
        $where = $this->request->request('where');
        $sort = $this->request->request('sort');
        $order = $this->request->request('order');
        $offset = $this->request->request('offset');
        $limit = $this->request->request('limit');

        $school_ids = $this->getSchoolIds($this->_user['id']);
        if (in_array('*', $school_ids)) {
            $total = \app\common\model\Student::alias('student')
                ->join('__SCHOOL__ school', 'school.id = student.school_id')
                ->join('__CLASSES__ class', 'class.id = student.class_id')
                ->join('__GRADE__ grade', 'grade.id = student.grade_id')
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = \app\common\model\Student::alias('student')
                ->join('__SCHOOL__ school', 'school.id = student.school_id')
                ->join('__CLASSES__ class', 'class.id = student.class_id')
                ->join('__GRADE__ grade', 'grade.id = student.grade_id')
                ->where($where)
                ->field('student.*,school.name school_name,class.name class_name,grade.name grade_name')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
        } else {
            $total = \app\common\model\Student::alias('student')
                ->join('__SCHOOL__ school', 'school.id = student.school_id')
                ->join('__CLASSES__ class', 'class.id = student.class_id')
                ->join('__GRADE__ grade', 'grade.id = student.grade_id')
                ->where($where)
                ->whereIn('student.school_id', $school_ids)
                ->order($sort, $order)
                ->count();

            $list = \app\common\model\Student::alias('student')
                ->join('__SCHOOL__ school', 'school.id = student.school_id')
                ->join('__CLASSES__ class', 'class.id = student.class_id')
                ->join('__GRADE__ grade', 'grade.id = student.grade_id')
                ->where($where)
                ->whereIn('student.school_id', $school_ids)
                ->field('student.*,school.name school_name,class.name class_name,grade.name grade_name')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
        }

        if ($list) {
            $list = collection($list)->append(['gender_text'])->toArray();
        }

        $result = array("total" => $total, "rows" => $list);

        $this->success('', $result);

    }

    public function getProfileByIds()
    {
        $ids = $this->request->request('ids/a');

        $list = \app\common\model\Student::alias('student')
            ->join('__SCHOOL__ school', 'school.id = student.school_id')
            ->whereIn('student.id', $ids)
            ->column('student.id,student.number,student.name,student.gender,student.avatar,school.name school_name', 'student.id');

        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['gender_text'] = User::getGender($value['gender']);

                if (!strstr($value['avatar'], 'http') && strpos($value['avatar'], 'uploads/') > -1) {
                    $list[$key]['avatar'] = config('app_url') . $value['avatar'];
                }
            }
        }

        $this->success('', $list);
    }

    public function studentsGroupBySchool()
    {
        $school_ids = $this->getSchoolIds($this->_user['id']);
        if (in_array('*', $school_ids)) {
            $result = \app\common\model\Student::alias('student')
                ->join('__SCHOOL__ school', 'school.id = student.school_id')
                ->field('student.school_id,count(student.id),school.name school_name')
                ->group('student.school_id')
                ->select();
        } else {
            $result = \app\common\model\Student::alias('student')
                ->join('__SCHOOL__ school', 'school.id = student.school_id')
                ->whereIn('student.school_id', $school_ids)
                ->field('student.school_id,count(student.id),school.name school_name')
                ->group('student.school_id')
                ->select();
        }

        $this->success('', $result);
    }

    public function setBed()
    {
        $ids = $this->request->request('ids/a');
        $result = \app\common\model\Student::whereIn('id', $ids)->update(['is_bed' => 1]);
        if ($result) {
            $this->success();
        } else {
            $this->error();
        }
    }

    public function resetBed()
    {
        $ids = $this->request->request('ids/a');
        $result = \app\common\model\Student::whereIn('id', $ids)->update(['is_bed' => 0]);
        if ($result) {
            $this->success();
        } else {
            $this->error();
        }
    }
}